package org.tote.model;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

@Entity
@Table(name = "audit")
public class Audit implements Serializable {

	private static final long serialVersionUID = 7557380793090914019L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id")
	private Long id;

	@Column(name = "type")
	@Enumerated(EnumType.STRING)
	private AuditOperationType type;

	@Column(name = "comment")
	private String comment;

	@Column(name = "date_created", insertable = false, updatable = false)
	@Temporal(TemporalType.TIMESTAMP)
	private Date dateCreated;

	@ManyToOne
	@JoinColumn(name = "user_id", updatable = false)
	private UserLight user;

	public Audit() {
	}

	public Audit(AuditOperationType type, String comment, UserLight user) {
		super();
		this.type = type;
		this.comment = comment;
		this.user = user;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public AuditOperationType getType() {
		return type;
	}

	public void setType(AuditOperationType type) {
		this.type = type;
	}

	public String getComment() {
		return comment;
	}

	public void setComment(String comment) {
		this.comment = comment;
	}

	public Date getDateCreated() {
		return dateCreated;
	}

	public void setDateCreated(Date dateCreated) {
		this.dateCreated = dateCreated;
	}

	public UserLight getUser() {
		return user;
	}

	public void setUser(UserLight user) {
		this.user = user;
	}

	@Override
	public String toString() {
		return "Audit [id=" + id + ", type=" + type + ", comment=" + comment + ", dateCreated="
				+ dateCreated + ", user=" + user + "]";
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((comment == null) ? 0 : comment.hashCode());
		result = prime * result + ((dateCreated == null) ? 0 : dateCreated.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime * result + ((type == null) ? 0 : type.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) return true;
		if (obj == null) return false;
		if (getClass() != obj.getClass()) return false;
		Audit other = (Audit) obj;
		if (comment == null) {
			if (other.comment != null) return false;
		} else if (!comment.equals(other.comment)) return false;
		if (dateCreated == null) {
			if (other.dateCreated != null) return false;
		} else if (!dateCreated.equals(other.dateCreated)) return false;
		if (id == null) {
			if (other.id != null) return false;
		} else if (!id.equals(other.id)) return false;
		if (type != other.type) return false;
		return true;
	}

}
